package com.isoft.controller;

import com.isoft.entity.DfmeCaneEntity;
import com.isoft.service.DfmeCaneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/api/flight-cancellation")
public class DfmeCaneController {
    private final DfmeCaneService dfmeCaneService;

    @Autowired
    public DfmeCaneController(DfmeCaneService dfmeCaneService) {
        this.dfmeCaneService = dfmeCaneService;
    }

    @PostMapping("/upload")
    public ResponseEntity<String> uploadFlightCancellation(@RequestParam("file") MultipartFile file) {
        try {
            String result = dfmeCaneService.parseAndSave(file);
            return ResponseEntity.ok(result);
        } catch (IOException e) {
            return ResponseEntity.badRequest().body("文件上传失败: " + e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("航班取消处理失败: " + e.getMessage());
        }
    }

    @PostMapping("/process")
    public ResponseEntity<String> processFlightCancellation(@RequestBody DfmeCaneEntity entity) {
        try {
            dfmeCaneService.parseAndSave(entity);
            return ResponseEntity.ok("航班取消处理成功. 航班ID: " + entity.getDflt().getFlightId());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("航班取消处理失败: " + e.getMessage());
        }
    }
}